﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../../Script/boot.js" type="text/javascript"></script>
    <script src="../FileUpload/swfupload/swfupload.js" type="text/javascript"></script>
    <style type="text/css">
        html, body
        {
            font-size: 12px;
            padding: 0;
            margin: 0;
            border: 0;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <form id="form1" method="post">
    <input name="id" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 3px;">
        <legend>基本信息</legend>
        <div style="padding: 5px;">
            <table>
                <tr>
                    <td style="width: 70px;">
                        商品编号：
                    </td>
                    <td style="width: 150px;">
                        <input id="commodityNoID" name="commodityNo" class="mini-textbox" vtype="int;rangeLength:5,7"
                            required="true" onvalidation="onCommodityNoValidation" />
                    </td>
                    <td style="width: 70px;">
                        商品名称：
                    </td>
                    <td style="width: 150px;">
                        <input name="commodityName" class="mini-textbox" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        产品规格：
                    </td>
                    <td>
                        <input name="specification" class="mini-textbox" required="true" />
                    </td>
                    <td>
                        包装比例：
                    </td>
                    <td>
                        <input name="packageScale" class="mini-textbox" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        包装单位：
                    </td>
                    <td>
                        <input name="packuint" class="mini-textbox" required="true" />
                    </td>
                    <td>
                        生产厂商：
                    </td>
                    <td>
                        <input id="factoryNo" name="factoryNo" class="mini-combobox" valuefield="factoryNo" textfield="factoryName"
                            url="AjaxService.aspx?method=GetFactorys" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        有&nbsp;效&nbsp;期：
                    </td>
                    <td>
                        <input name="expirationDate" class="mini-textbox" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        产品图片：
                    </td>
                    <td>
                        <input id="fileupload1" class="mini-fileupload" style="" name="Fdata" flashurl="../FileUpload/swfupload/swfupload.swf"
                            limittype="*.jpg;*.jpeg;*.bmp;*.gif;" onuploaderror="onUploadError" onuploadsuccess="onUploadSuccess"
                            uploadurl="../FileUpload/upload.aspx" />
                    </td>
                    <td colspan="2">
                        <a class="mini-button" onclick="startUpload()" style="width: 60px;">上传</a>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <input name="commodityPicPath" class="mini-hidden" required="true" />
                    </td>
                </tr>
                <tr>
                    <td style="height: 150px">
                        缩略图：
                    </td>
                    <td colspan="3">
                        <div style="text-align: center;">
                            <img id="commodityPic" src="" alt="" width="100" height="120" /></div>
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" onclick="onOk" style="width: 60px; margin-right: 20px;">确定</a>
        <a class="mini-button" onclick="onCancel" style="width: 60px;">取消</a>
    </div>
    </form>
    <script type="text/javascript">
        mini.parse();

        var form = new mini.Form("form1");
        getRoleName();
        function SaveData() {
            var o = form.getData();

            form.validate();
            if (form.isValid() == false) return;

            var json = mini.encode([o]);
            $.ajax({
                url: "AjaxService.aspx?method=SaveCommodity",
                data: { data: json },
                cache: false,
                success: function (text) {
                    CloseWindow("save");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }

        ////////////////////
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);

                $.ajax({
                    url: "AjaxService.aspx?method=GetCommodity&id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);

                        var obj = mini.get("commodityNoID");
                        obj.disable();
                        $("#commodityPic").attr("src", "../../" + o.commodityPicPath);
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }
        function onOk(e) {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        //        //////////////////////////////////
        //        function onDeptChanged(e) {
        //            var deptCombo = mini.getbyName("dept_id");
        //            var positionCombo = mini.getbyName("position");
        //            var dept_id = deptCombo.getValue();

        //            positionCombo.load("../data/AjaxService.aspx?method=GetPositionsByDepartmenId&id=" + dept_id);
        //            positionCombo.setValue("");
        //        }

        function onUploadSuccess(e) {

            //alert("上传成功：" + e.serverData);
            mini.alert("上传成功!");
            this.setText(e.serverData);
            mini.getbyName("commodityPicPath").setValue(e.serverData);
            $("#commodityPic").attr("src", "../../" + e.serverData);
        }
        function onUploadError(e) {

        }

        function startUpload() {
            var fileupload = mini.get("fileupload1");

            fileupload.startUpload();
        }

        //////////////////////////////
        function isCommodityNoExist(value) {
            var result = true;
            $.ajax({
                url: "AjaxService.aspx?method=CommodityNoValidation&commodityNo=" + value,
                cache: false,
                async: false,
                success: function (text) {
                    if (text == "0") {
                        result = false;
                    } else {
                        result = true;
                    }
                }
            });
            if (result) {
                return true;
            } else {
                return false;
            }
        }

        function onCommodityNoValidation(e) {
            if (e.isValid) {
                var obj = mini.get("commodityNoID");
                if (obj.enabled == false) {
                    e.isValid = true;
                    return;
                }
                var result = isCommodityNoExist(e.value);
                if (result == false) {
                    e.errorText = "商品编号已经存在";
                    e.isValid = false;
                }
            }
        }

        function getRoleName() {
            var urlGetRoleString = "../Common/UserSessionTool.aspx?method=ResponseWriteRoleName";
            $.ajax({
                url: urlGetRoleString,
                cache: false,
                success: function (text) {
                    if (text != "超级管理员") {
                        var trSuper = mini.get("factoryNo");
                        var el = trSuper.getEl();
                        $(el).parent().parent().hide();
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                    return false;
                }
            });
        }
    </script>
</body>
</html>
